<template>
  <div :class="{ 'hidden': hidden }" class="pagination-container">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"  :layout="layout" background
                   :page-sizes="pageSizes"
                   :total="total"
                   style="transform: translateY(-8%);background-color: rgb(237, 245, 255);text-align: right;padding-top: 10px"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </div>
</template>

<script lang="ts" setup>
import {scrollTo} from '@/utils/scroll-to'

const emits = defineEmits(["update:page", "update:limit", "pagination"])
const props = defineProps({
  total: {
    required: true,
    type: Number
  },
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 10
  },
  pageSizes: {
    type: Array,
    default() {
      return [10, 30, 50, 100, 200, 500]
    }
  },
  // 移动端页码按钮的数量端默认值5 (大于等于 5 且小于等于 21 的奇数)
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  }

})

let currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emits('update:page', val)
  }
})

let pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emits('update:limit', val)
  }
})

function handleSizeChange(val) {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1
  }
  pageSize.value = val
  emits('pagination', {page: currentPage.value, limit: val})
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

function handleCurrentChange(val) {
  emits('pagination', {page: val, limit: pageSize.value})
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}
</script>
<script lang="ts">
export default {
  name: 'Pagination'
}
</script>
<style scoped>
.pagination-container {
  background: #fff;
  padding: 0;
  margin: 0;
}

.pagination-container.hidden {
  display: none;
}
</style>
